﻿using LiveCharts;
using LiveCharts.Configurations;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ModubsRTUTempactureCurve.Models
{
    public class ItemModel:ObservableObject
    {
        public int SlaveId { get; set; }

        private double temperature;
        public double Temperature
        {
            get { return temperature; }
            set { temperature = value;
                DateTime dt = DateTime.Now;
                this.TempValueList.Add(new lvcModel { Value = value, DateTiem = dt });
                if(TempValueList.Count > 11)
                {
                    TempValueList.RemoveAt(0);
                }
                UpdateRange();
                RaisePropertyChanged(); }
        }

        private double humidity;

        public double Humidity
        {
            get { return humidity; }
            set { humidity = value; RaisePropertyChanged();
                DateTime dt = DateTime.Now;
                this.HumiValueList.Add(new lvcModel { Value = value, DateTiem = dt });
                if(HumiValueList.Count > 11)
                {
                    HumiValueList.RemoveAt(0);
                }
                UpdateRange(); RaisePropertyChanged();
            }
        }

        public ChartValues<lvcModel> TempValueList { get; set; } = new ChartValues<lvcModel>();
        public ChartValues<lvcModel> HumiValueList { get; set; } = new ChartValues<lvcModel>();

        //private ChartValues<lvcModel> _tempValueList = new ChartValues<lvcModel>();

        //public ChartValues<lvcModel> TempValueList
        //{
        //    get { return _tempValueList; }
        //    set { _tempValueList = value;
        //        RaisePropertyChanged();
        //    }
        //}

        //private ChartValues<lvcModel> _humiValueList = new ChartValues<lvcModel>();

        //public ChartValues<lvcModel> HumiValueList
        //{  
        //    get { return _humiValueList; }
        //    set { _humiValueList = value; }
        //}


        public ItemModel()
        {
            CartesianMapper<lvcModel> mapper = Mappers.Xy<lvcModel>().X(model => model.DateTiem.Ticks).Y(model => model.Value);
            Charting.For<lvcModel>(mapper);
            XFormatter = new Func<double, string>(x => new DateTime((long)x).ToString("mm:ss"));
            UpdateRange();
        }

        public Func<double,string>XFormatter { get; set; }

        private double _axisMin;

        public double AxisMin
        {
            get { return _axisMin; }
            set { _axisMin = value; RaisePropertyChanged(); }
        }

        private double _axisMax;

        public double AxisMax
        {
            get { return _axisMax; }
            set { _axisMax = value; RaisePropertyChanged(); }
        }

        public void UpdateRange()
        {
            AxisMax = DateTime.Now.Ticks;
            AxisMin = DateTime.Now.Ticks - TimeSpan.FromSeconds(9).Ticks;
        }
        //间隔
        public double AxisUnit { get; set; } = TimeSpan.TicksPerSecond;
        public double AxisStep { get; set; } = TimeSpan.FromSeconds(1).Ticks;
    }
}
